﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using XbsWebAPI.EntityFrameworkCore;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.Enums;

namespace XbsWebAPI.Core.Repositories
{
    /// <summary>
    /// 营位领域模型 实现
    /// </summary>
    public class CampRepository : ICampRepository
    {
        private readonly DataContext _dbContext;

        public CampRepository(DataContext dataContext)
        {
            _dbContext = dataContext;
        }

        public Camp Create(Camp entity)
        {
            entity.Id = Guid.NewGuid();
            entity.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); ;
            _dbContext.Camps.Add(entity);

            if(entity.CampImages != null && entity.CampImages.Count > 0)
            {
                foreach (var image in entity.CampImages)
                {
                    image.Id = Guid.NewGuid();
                    image.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    image.CampId = entity.Id;
                    _dbContext.CampImages.Add(image);
                }
            }

            _dbContext.SaveChanges();

            return entity;
        }

        public Task<Camp> CreateAsync(Camp entity)
        {
            return Task.FromResult(Create(entity));
        }

        public Camp Delete(Guid id)
        {
            var model = _dbContext.Camps.FirstOrDefault(x => x.Id == id);
            if (model == null)
                return null;
            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return model;
        }

        public Task<Camp> DeleteAsync(Guid id)
        {
            return Task.FromResult(Delete(id));
        }

        public IQueryable<Camp> Query(Expression<Func<Camp, bool>> filter)
        {
            var query = _dbContext.Camps.Where(filter);

            return query;
        }

        public IQueryable<Camp> AsQueryable()
        {
            var query = _dbContext.Camps.AsQueryable();

            return query;
        }

        public Camp QueryById(Guid id)
        {
            return _dbContext.Camps.FirstOrDefault(x => x.Id == id);
        }

        public Camp Update(Camp entity)
        {
            var model = _dbContext.Camps.AsNoTracking().FirstOrDefault(x => x.Id == entity.Id);
            if (model == null)
                return null;

            //处理图片
            var campsImages = _dbContext.CampImages.AsNoTracking().Where(a => a.CampId == entity.Id);
            _dbContext.CampImages.RemoveRange(campsImages);

            var images = entity.CampImages;

            foreach (var image in images)
            {
                image.Id = Guid.NewGuid();
                image.CampId = entity.Id;
                image.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                _dbContext.CampImages.Add(image);
            }

            entity.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            entity.CreationDate = model.CreationDate;
            _dbContext.Camps.Update(entity);
            _dbContext.SaveChanges();

            return entity;
        }

        public Task<Camp> UpdateAsync(Camp entity)
        {
           return Task.FromResult(Update(entity));
        }
    }
}
